<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

require_once ( LIB_DIR.'search/abstractsearch.php');
/**
 * Description of shopsearch
 *
 * @author jszhang
 */
class ProductSearch  extends Search {

    public function ProductSearch( $host='127.0.0.1', $port=3312 ) {
        parent::__construct($host, $port);

        //override some member vars
        //$this->m_index = array( "shop_info_index", "shop_info_stemmed" );
        $this->m_index = array( "product_info_index", "product_info_stemmed" );
    }

    /**
     * override protected functions
     */

    //after some default value changed, invoke this function
    protected function initialize() {
		//clear all filters
		$this->m_sphinxClient->ResetFilters();
		$this->m_sphinxClient->ResetOverrides();
		$this->m_sphinxClient->ResetGroupBy();

        $this->m_sphinxClient->SetConnectTimeout ( 1 );
        $this->m_sphinxClient->SetMatchMode ( $this->m_matchMode );
        $this->m_sphinxClient->SetWeights ( array ( 100, 1 ) );

        //        if ( count($filtervals) )
        //            $cl->SetFilter ( $filter, $filtervals );
        //        if ( $groupby )
        //            $cl->SetGroupBy ( $groupby, SPH_GROUPBY_ATTR, $groupsort );
        //        if ( $sortby )
        //            $cl->SetSortMode ( SPH_SORT_EXTENDED, $sortby );

        //        if ( $sortexpr )
        //            $cl->SetSortMode ( SPH_SORT_EXPR, $sortexpr );

        //        if ( $distinct )
        //            $cl->SetGroupDistinct ( $distinct );
        //        if ( $select )
        //            $cl->SetSelect ( $select );

        $this->m_sphinxClient->SetRankingMode ( $this->m_ranker );
        $this->m_sphinxClient->SetArrayResult ( $this->m_isArrayResult );
        $this->m_perPage = PRODUCT_INFO_PER_PAGE;
    }

    //put your code here
    protected function printResult( $res ) {
        $result = array();

        $result['total'] = $res['total'];
        $result['total_found'] = $res['total_found'];
        $result['time'] = $res['time'];

        if ( isset($res["matches"]) && is_array( $res["matches"] ) ) {
            $result['matchIds'] = array();
            foreach ( $res["matches"] as $docinfo ) {

                $result['matchIds'][] = $docinfo['id'];
            }
        }

        return $result;
    }

	//assemble group by result
	protected function printGroupByResult( $res, $groupField ){
		$result = array();

        $result['total'] = $res['total'];
        $result['total_found'] = $res['total_found'];

		if ( isset($res["matches"]) && is_array( $res["matches"] ) ) {
            $result['matches'] = array();
            foreach ( $res["matches"] as $docinfo ) {
                $result['matches'][$docinfo['attrs']['@groupby']] = array(
					'count' => $docinfo['attrs']['@count']
				);
            }
        }
		return $result;
	}
	
	
	/**********************************************************
	 *	Seal Created by 2010-6-19
	 **********************************************************/
	public function GetHotProduct( $count )
	{
		
		$this->m_sphinxClient->SetLimits( 0, $count );
		$this->m_sphinxClient->SetFilter( 'status', array(9), true );
		$this->m_sphinxClient->SetSortMode ( SPH_SORT_ATTR_DESC, 'review_count' );
        $re = $this->doSearchAttributeOnly();
        
        if( $re === false || !isset($re['matches']) )
        	return false;

        return $this->GetIdsFromRes( $re['matches'] );
	}
	
	/**********************************************************
	 *	Seal Created by 2010-6-20
	 **********************************************************/
	public function GetHotProductSameShop( $shopId, $count )
	{
		
		$this->m_sphinxClient->SetLimits( 0, $count  );
		$this->m_sphinxClient->SetFilter( 'status', array(9), true );
		$this->m_sphinxClient->SetFilter( 'shop_id', array($shopId) );
		$this->m_sphinxClient->SetSortMode ( SPH_SORT_ATTR_DESC, 'review_count' );
        $re = $this->doSearchAttributeOnly();
        
        if( $re === false || !isset($re['matches']) )
        	return false;

        return $this->GetIdsFromRes( $re['matches'] );
	}
	
	/**********************************************************
	 *	Seal Created by 2010-6-20
	 **********************************************************/
	public function GetHotProductSameCategory( $cateId, $count )
	{
		
		$this->m_sphinxClient->SetLimits( 0, $count );
		$this->m_sphinxClient->SetFilter( 'status', array(9), true );
		$this->m_sphinxClient->SetFilter( 'cate_id', array($cateId) );
		$this->m_sphinxClient->SetSortMode ( SPH_SORT_ATTR_DESC, 'review_count' );
        $re = $this->doSearchAttributeOnly();
        
        if( $re === false || !isset($re['matches']) )
        	return false;

        return $this->GetIdsFromRes( $re['matches'] );
	}
	
	/**********************************************************
	 *	Seal Created by 2010-6-20
	 **********************************************************/
	public function GetHotProductByPlatid( $platId, $count )
	{
		
		$this->m_sphinxClient->SetLimits( 0, $count );
		$this->m_sphinxClient->SetFilter( 'status', array(9), true );
		$this->m_sphinxClient->SetFilter( 'plat_id', array($platId) );
		$this->m_sphinxClient->SetSortMode ( SPH_SORT_ATTR_DESC, 'review_count' );
        $re = $this->doSearchAttributeOnly();
        
        if( $re === false || !isset($re['matches']) )
        	return false;

        return $this->GetIdsFromRes( $re['matches'] );
	}
	
}
?>
